package com.huawei.updatesdk.service.otaupdate;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import com.huawei.fastengine.BuildConfig;
import com.huawei.updatesdk.a.a.b.a.a.a;
import com.huawei.updatesdk.a.a.c.e;
import com.huawei.updatesdk.service.appmgr.bean.ApkUpgradeInfo;
import com.huawei.updatesdk.service.e.b;
import com.huawei.updatesdk.service.e.c;
import java.util.ArrayList;
import java.util.List;

public class d extends AsyncTask<Void, Void, com.huawei.updatesdk.a.b.d.a.d> {
    private Context a;
    private CheckUpdateCallBack b;
    private String c = null;
    private UpdateParams d;
    private Toast e;
    private boolean f = false;

    public d(Context context, UpdateParams updateParams, CheckUpdateCallBack checkUpdateCallBack) {
        this.d = updateParams;
        this.a = context;
        this.b = checkUpdateCallBack;
        if (!TextUtils.isEmpty(updateParams.getServiceZone())) {
            g.a().a(updateParams.getServiceZone());
        }
        if (TextUtils.isEmpty(g.a().c())) {
            g.a().c(updateParams.getAppStorePkgName());
        }
    }

    private com.huawei.updatesdk.a.b.d.a.d a(Context context, String str) {
        Object a = c.a(str, context);
        if (a == null && TextUtils.isEmpty(this.d.getTargetPkgName())) {
            a.d("CheckOtaAndUpdataTask", "get app packageInfo failed,packageName:" + str);
            return null;
        }
        if (a == null) {
            a = new PackageInfo();
            a.packageName = str;
            a.versionName = BuildConfig.VERSION_NAME;
            a.versionCode = 1;
            ApplicationInfo applicationInfo = new ApplicationInfo();
            applicationInfo.targetSdkVersion = 19;
            a.applicationInfo = applicationInfo;
        }
        List arrayList = new ArrayList();
        arrayList.add(a);
        com.huawei.updatesdk.a.b.d.a.c a2 = com.huawei.updatesdk.service.appmgr.bean.a.a(arrayList);
        a2.a(0);
        return com.huawei.updatesdk.service.d.a.c.a(a2);
    }

    private ApkUpgradeInfo a(String str, List<ApkUpgradeInfo> list) {
        if (list == null || TextUtils.isEmpty(str)) {
            return null;
        }
        for (ApkUpgradeInfo apkUpgradeInfo : list) {
            if (str.equals(apkUpgradeInfo.getPackage_()) && apkUpgradeInfo.getOldVersionCode_() < apkUpgradeInfo.getVersionCode_()) {
                return apkUpgradeInfo;
            }
        }
        return null;
    }

    private void a() {
        if (this.e != null) {
            this.e.cancel();
        }
    }

    private void a(Context context, ApkUpgradeInfo apkUpgradeInfo) {
        if (context != null) {
            Intent intent = new Intent(context, AppUpdateActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("app_update_parm", apkUpgradeInfo);
            bundle.putSerializable("app_must_btn", Boolean.valueOf(this.d.isMustBtnOne()));
            intent.putExtras(bundle);
            if (!(context instanceof Activity)) {
                intent.setFlags(268435456);
            }
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                a.d("CheckOtaAndUpdataTask", "go AppUpdateActivity error: " + e.toString());
            }
        }
    }

    private void b() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(com.huawei.updatesdk.a.a.c.a.a.b()).append(VERSION.SDK_INT).append(Build.MODEL).append(com.huawei.updatesdk.a.a.c.a.a.c(com.huawei.updatesdk.a.b.a.a.a().b())).append(g.a().d()).append(b.a().b()).append(com.huawei.updatesdk.a.a.c.a.a.e().f()).append(com.huawei.updatesdk.a.a.c.a.a.e().g());
        Object c = e.c(stringBuilder.toString());
        Object obj = !TextUtils.equals(c, com.huawei.updatesdk.service.a.a.a().c()) ? 1 : null;
        if (obj != null) {
            com.huawei.updatesdk.service.a.a.a().a(c);
        }
        long g = com.huawei.updatesdk.service.a.a.a().g();
        long b = com.huawei.updatesdk.service.a.a.a().b();
        if (obj != null || TextUtils.isEmpty(g.a().d()) || Math.abs(g - b) >= 7 || TextUtils.isEmpty(com.huawei.updatesdk.service.a.a.a().d())) {
            com.huawei.updatesdk.a.b.d.a.d a = com.huawei.updatesdk.service.d.a.c.a(com.huawei.updatesdk.service.a.c.f());
            if ((a instanceof com.huawei.updatesdk.service.a.d) && a.b() == 0 && a.a() == 0) {
                com.huawei.updatesdk.service.a.a.a().b(((com.huawei.updatesdk.service.a.d) a).e());
            }
        }
    }

    private void b(com.huawei.updatesdk.a.b.d.a.d dVar) {
        if (this.b != null) {
            Intent intent = new Intent();
            intent.putExtra(UpdateKey.STATUS, 6);
            if (dVar.c() != null) {
                intent.putExtra(UpdateKey.FAIL_CODE, dVar.c().ordinal());
            }
            intent.putExtra(UpdateKey.FAIL_REASON, dVar.d());
            intent.putExtra(UpdateKey.REQUEST_SIGN, com.huawei.updatesdk.service.a.a.a().d());
            this.b.onUpdateInfo(intent);
            this.b.onUpdateStoreError(dVar.a());
        }
    }

    protected com.huawei.updatesdk.a.b.d.a.d a(Void... voidArr) {
        a.a("CheckOtaAndUpdataTask", "CheckOtaAndUpdataTask doInBackground");
        if (VERSION.SDK_INT >= 24 || com.huawei.updatesdk.a.a.c.a.a.c() == 0 || c.a(com.huawei.updatesdk.a.b.a.a.a().b()) != c.a.a || c.c()) {
            com.huawei.updatesdk.a.b.d.a.c.a(com.huawei.updatesdk.a.a.c.a.a.a(f.c(this.a, "upsdk_store_url")));
            if (TextUtils.isEmpty(g.a().b())) {
                g.a().a(this.a);
            }
            this.c = this.d.getTargetPkgName();
            if (TextUtils.isEmpty(this.c)) {
                this.c = this.a.getPackageName();
            }
            com.huawei.updatesdk.service.a.a.a().c("updatesdk_" + this.c);
            com.huawei.updatesdk.service.a.a.a().d(g.a().d());
            b();
            return a(this.a, this.c);
        }
        a.a("CheckOtaAndUpdataTask", "api <24,HiApp not installed and no permission");
        return null;
    }

    protected void a(com.huawei.updatesdk.a.b.d.a.d dVar) {
        a();
        if (dVar != null) {
            Intent intent;
            ApkUpgradeInfo apkUpgradeInfo = null;
            if (dVar.a() == 0 && dVar.b() == 0) {
                apkUpgradeInfo = a(this.c, ((com.huawei.updatesdk.service.appmgr.bean.b) dVar).list_);
                if (apkUpgradeInfo == null && this.b != null) {
                    intent = new Intent();
                    intent.putExtra(UpdateKey.STATUS, 3);
                    intent.putExtra(UpdateKey.REQUEST_SIGN, com.huawei.updatesdk.service.a.a.a().d());
                    this.b.onUpdateInfo(intent);
                }
            } else {
                b(dVar);
                a.d("CheckOtaAndUpdataTask", "get app update msg failed,responseCode is " + dVar.a());
            }
            if (!(apkUpgradeInfo == null || TextUtils.isEmpty(apkUpgradeInfo.getFullDownUrl_()))) {
                apkUpgradeInfo.setDownurl_(apkUpgradeInfo.getFullDownUrl_());
            }
            if (apkUpgradeInfo != null) {
                a.a("CheckOtaAndUpdataTask", "check store client update success!" + apkUpgradeInfo.getVersionCode_() + ",version:" + apkUpgradeInfo.getVersion_());
                if (this.b != null) {
                    intent = new Intent();
                    intent.putExtra(UpdateKey.INFO, apkUpgradeInfo);
                    intent.putExtra(UpdateKey.STATUS, 7);
                    intent.putExtra(UpdateKey.REQUEST_SIGN, com.huawei.updatesdk.service.a.a.a().d());
                    this.b.onUpdateInfo(intent);
                }
                if (this.d.isShowImmediate()) {
                    a(this.a, apkUpgradeInfo);
                }
            } else if (!this.f && TextUtils.isEmpty(this.d.getTargetPkgName())) {
                Toast.makeText(this.a, f.b(this.a, "upsdk_update_check_no_new_version"), 0).show();
            }
        } else if (this.b != null) {
            Intent intent2 = new Intent();
            intent2.putExtra(UpdateKey.STATUS, 3);
            intent2.putExtra(UpdateKey.REQUEST_SIGN, com.huawei.updatesdk.service.a.a.a().d());
            this.b.onUpdateInfo(intent2);
        }
    }

    public void a(boolean z) {
        this.f = z;
    }

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return a((Void[]) objArr);
    }

    protected /* synthetic */ void onPostExecute(Object obj) {
        a((com.huawei.updatesdk.a.b.d.a.d) obj);
    }

    protected void onPreExecute() {
        super.onPreExecute();
        c.a().a(this.b);
        if (!this.f && TextUtils.isEmpty(this.d.getTargetPkgName())) {
            this.e = Toast.makeText(this.a, f.b(this.a, "upsdk_checking_update_prompt"), 1);
            this.e.show();
        }
    }
}
